package cao.oschina.api;


import cao.oschina.app.Config;
import cao.oschina.bean.Result;
import hd.util.StringUtils;
import hd.util.http.HttpClient;
import hd.util.http.HttpRequestListener;
import hd.util.http.HttpResponse;
import hd.util.http.entity.HttpEntityBody;

/**
 * Api
 * <p/>
 * Created by Haidy on 15/3/13.
 */
public class Api {

    /**
     * 登录
     *
     * @param email         email
     * @param pwd           pwd
     * @param onApiListener apiListener
     */
    public static void login(String email, String pwd, final OnApiListener<HttpResponse> onApiListener) {
        HttpEntityBody entityBody = new HttpEntityBody();
        entityBody.addForm("keep_login", "1");
        entityBody.addForm("username", email);
        entityBody.addForm("pwd", pwd);
        new HttpClient().httpPost(URLs.LOGIN, null, entityBody, new HttpRequestListener() {
            @Override
            public void success(HttpResponse response) {
                Result result = Result.parser(response.text);
                if ("1".equals(result.errorCode)) {
                    onApiListener.result(true, response);
                } else {
                    onApiListener.result(false, response);
                }
            }

            @Override
            public void failure(HttpResponse response) {
                onApiListener.result(false, response);
            }
        });
    }

    private static void reLogin(String email, String pwd, String cookie, final OnReLoginListener onReLoginListener) {
        if (StringUtils.isEmpty(cookie)) {
            login(email, pwd, new OnApiListener<HttpResponse>() {
                @Override
                public void result(boolean statue, HttpResponse data) {
                    if (statue) {
                        onReLoginListener.load(data.getCookie());
                    } else {
                        onReLoginListener.failure(data);
                    }
                }
            });
        } else {
            onReLoginListener.load(cookie);
        }
    }

    /**
     * 加载数据
     *
     * @param isLoaded      是否加载
     * @param config        配置
     * @param url           url
     * @param onApiListener apiListener
     */
    public static void loadData(final boolean isLoaded, final Config config, final String url, final OnApiListener<HttpResponse> onApiListener) {
        reLogin(config.getEmail(), config.getPwd(), config.getCookies(), new OnReLoginListener() {
            @Override
            public void load(final String cookie) {
                new HttpClient().httpGet(url, cookie, new HttpRequestListener() {
                    @Override
                    public void success(HttpResponse response) {
                        config.saveConfig(Config.COOKIES, cookie);
                        onApiListener.result(true, response);
                    }

                    @Override
                    public void failure(HttpResponse response) {
                        if (isLoaded) {
                            onApiListener.result(false, response);
                        } else {
                            config.removeKey(Config.COOKIES);
                            loadData(true, config, url, onApiListener);
                        }
                    }
                });
            }

            @Override
            public void failure(HttpResponse httpResponse) {
                onApiListener.result(false, httpResponse);
            }
        });
    }

    private interface OnReLoginListener {

        void load(String cookie);

        void failure(HttpResponse httpResponse);
    }

    /**
     * Api接口
     *
     * @param <T>
     */
    public interface OnApiListener<T> {

        void result(boolean statue, T data);
    }
}
